package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.ApplicationScope;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 后台代码
* */
/**
 * <p>
 * 讲师 前端控制器 讲师模块
 * </p>
 *
 * @author 若chen
 * @since 2022-04-12
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin //解决跨域问题 ---》 （前后端请求端口号不一样） （2）使用网关也能解决
public class EduTeacherController {

    //把service注入到controller
    @Autowired
    private EduTeacherService eduTeacherService;

    //1.查所有的讲师
    //rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        //调用service的方法查询所有的操作
        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("items",list);
    }

    //2.根据id删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")//id=需要通过路径进行传值  @PathVariable 通过路径传值  @ApiParam--解释
    public R removeById(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){

        boolean flag = eduTeacherService.removeById(id);

        if(flag){
            return R.ok();
        }else {
            return R.error();
        }

    }

    //3.分页查询讲师的方法 current -代表当前页 limit 代表每页显示的记录数
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);


        try{
            //int i = 1/0;
        }catch (Exception e){
            //执行自定义异常
            throw new GuliException(20001,"执行了自定义异常处理....");

        }

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();//返回总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//没页数数据list集合

        //第一中方法
      /*Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return  R.ok().data(map);*/
        //第二种方法
         return R.ok().data("total",total).data("rows",records);
    }

    //4.条件查询带分页查询方法
    @ApiOperation(value = "条件查询分页方法")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page
        Page<EduTeacher> pageCondition = new Page<>(current, limit);

        //QueryWrapper,构建
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询，动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件是否为空，拼接条件
        if (!StringUtils.isEmpty(level)) {
            //等于
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(name)) {
            //模糊查询
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(begin)) {
            //ge 大于等于
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //le小于等于
            wrapper.le("gmt_create", end);
        }
        //降序
        wrapper.orderByDesc("gmt_create");

        //调用方法，实现分页查询
        eduTeacherService.page(pageCondition, wrapper);

        long total = pageCondition.getTotal();//获取总记录数
        List<EduTeacher> records = pageCondition.getRecords();//获取分页后的list集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    //5.添加讲师接口的方法
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //6.根据id进行查询
    @ApiOperation(value = "根据id查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher (@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);

        return R.ok().data("teacher",eduTeacher);
    }

    //7.讲师修改功能
    @ApiOperation(value = "根据id讲师修改功能")
    @PostMapping(value = "updateTeacher")
    public R updateTeacher (@RequestBody EduTeacher eduTeacher){

        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }


}

